package com.example.ticket.demo.service.impl;

import java.util.List;

import com.example.ticket.demo.dao.TicketMapper;
import com.example.ticket.demo.model.table.Ticket;
import com.example.ticket.demo.service.ITicketService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service("ticketService")
public class TicketServiceImpl implements ITicketService {

    @Autowired
    private TicketMapper ticketMapper;

    @Override
    public int deleteByPrimaryKey(String id) {
        return ticketMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Ticket record) {
        return ticketMapper.insertSelective(record);
    }

    @Override
    public int insertSelective(Ticket record) {
        return ticketMapper.insertSelective(record);
    }

    @Override
    public Ticket selectByPrimaryKey(String id) {
        return ticketMapper.selectByPrimaryKey(id);
    }

    @Override
    public Ticket selectByType(String type) {
        return ticketMapper.selectByType(type);
    }

    @Override
    public int updateByPrimaryKeySelective(Ticket record) {
        return ticketMapper.updateByPrimaryKey(record);
    }

    @Override
    public int updateByPrimaryKey(Ticket record) {
        return ticketMapper.updateByPrimaryKey(record);
    }

    @Transactional
    @Override
    public int updateTicketSale(Ticket record) {
        Ticket t = ticketMapper.selectByTypeForUpdate(record.getType());
        log.info(">>>>>> updateTicketSale: " + t);
        if (t.getTicketCount() > 0) {
            record.setVersion(t.getVersion());
            return ticketMapper.updateTicketSale(record);
        }
        return 0;
    }

    @Override
    public List<Ticket> selectList(Ticket record) {
        return ticketMapper.selectList(record);
    }

}
